/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package convertforgnuplot;

import org.apache.commons.math.fraction.BigFraction;
import java.math.BigInteger;

import java.io.Writer;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;

import java.io.Reader;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.InputStreamReader;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Vector;
import java.util.Scanner;

/**
 *
 * @author mdm32
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws FileNotFoundException, IOException {
        // TODO code application logic here
        // Process CLI arguments for INPUT ONLY
        Scanner scanner = null;
        String input_file = null;
        String output_file = null;
        BufferedWriter out = null;

        try {
            switch (args.length) {
                case 2:
                    input_file = args[0];
                    scanner = new Scanner(new BufferedReader(new FileReader(input_file)));
                    output_file = args[1];
                    break;
                case 1:
                    input_file = args[0];
                    scanner = new Scanner(new BufferedReader(new FileReader(input_file)));
                    output_file = input_file + "gplot";
                    break;
            }

            FileWriter fstream = new FileWriter(output_file);
            out = new BufferedWriter(fstream);
            // declare some temporary variables
            String cx1n, cx1d, cx2n, cx2d;
            String cy1n, cy1d, cy2n, cy2d;

            BigFraction cx1, cy1, cx2, cy2;
            // read in all data from file

            while (scanner.hasNext()) {
                String line = scanner.nextLine();
                Scanner line_scanner = new Scanner(line);
                String value = null;
                java.util.regex.MatchResult matcher = null;

                value = line_scanner.findInLine("(\\d+)(\\s*/\\s*(\\d+))?");
                matcher = line_scanner.match();
                cx1n = matcher.group(1);
                cx1d = matcher.group(3);
                if (cx1d == null || cx1d.equals("")) {
                    cx1d = "1";
                }

                value = line_scanner.findInLine("(\\d+)(\\s*/\\s*(\\d+))?");
                matcher = line_scanner.match();
                cy1n = matcher.group(1);
                cy1d = matcher.group(3);
                if (cy1d == null || cy1d.equals("")) {
                    cy1d = "1";
                }

                value = line_scanner.findInLine("(\\d+)(\\s*/\\s*(\\d+))?");
                matcher = line_scanner.match();
                cx2n = matcher.group(1);
                cx2d = matcher.group(3);
                if (cx2d == null || cx2d.equals("")) {
                    cx2d = "1";
                }

                value = line_scanner.findInLine("(\\d+)(\\s*/\\s*(\\d+))?");
                matcher = line_scanner.match();
                cy2n = matcher.group(1);
                cy2d = matcher.group(3);
                if (cy2d == null || cy2d.equals("")) {
                    cy2d = "1";
                }

                cx1 = new BigFraction(new BigInteger(cx1n), new BigInteger(cx1d));
                cy1 = new BigFraction(new BigInteger(cy1n), new BigInteger(cy1d));
                cx2 = new BigFraction(new BigInteger(cx2n), new BigInteger(cx2d));
                cy2 = new BigFraction(new BigInteger(cy2n), new BigInteger(cy2d));

                out.write(cx1.doubleValue() + " " + cy1.doubleValue() + " " + cx2.doubleValue() + " " + cy2.doubleValue() + "\n");
            }
        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
        } finally {
            if (scanner != null) {
                scanner.close();
            }

            if (out != null) {
                out.close();
            }
        }
    }
}
